package com.test;


import java.net.*;
import java.util.Enumeration;

/**
 * <p>
 * <code>Demo01</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-04-20 9:31
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class Demo01 {
    public static void main(String[] args) throws SocketException, UnknownHostException {
        String osName = System.getProperty("os.name");
        String s = osName.toLowerCase();
        System.out.println(s);
        if (s.contains("windows")){
            System.out.println("windows系统");
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();
                // 去除回环接口，子接口，未运行和接口
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                }
                String ip = "";
                String mac = "";
                String niName = "";
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress ia = addresses.nextElement();
                    // 去除本地回环地址，子接口，未运行和地址
                    if (ia != null && !ia.isLoopbackAddress() && ia.isSiteLocalAddress() && !ia.isAnyLocalAddress()) {
                        // 判断是否是ip v4地址
                        if (ia instanceof Inet4Address) {
                            ip = ia.getHostAddress();
                            niName = netInterface.getName();
                            mac = getMac(ia);
                                System.out.println("当前网卡："+niName);
                                System.out.println("当前主机ip："+ip);
                                //3C-95-09-41-40-BB
                                System.out.println("当前主机MAC："+mac);

                        }
                    }
                }
            }

        }else if (s.contains("linux")){
            System.out.println("linux系统");
            // 先获取ip
            InetAddress iad = InetAddress.getLocalHost();
            String localIp = iad.getHostAddress();

            // 遍历网卡
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();
                // 去除回环接口，子接口，未运行和接口
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                }

                String ip = "";
                String mac = "";
                String niName = "";
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress ia = addresses.nextElement();
                    if (ia != null && !ia.isLoopbackAddress() && ia.isSiteLocalAddress() && !ia.isAnyLocalAddress()) {
                        // 判断是否是ip v4地址且是否和已获取的ip一致
                        if (ia instanceof Inet4Address && ia.getHostAddress().equals(localIp)) {
                            ip = ia.getHostAddress();
                            // 获取MAC地址
                            mac = getMac(ia);
                            niName = netInterface.getName();

                                System.out.println("当前网卡："+niName);
                                System.out.println("当前主机ip："+ip);
                                System.out.println("当前主机MAC："+mac);
                                return;

                        }
                    }
                }
            }
        }
    }

    public static String getMac(InetAddress ia){
        try {
            //获取网卡，获取地址
            byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
            StringBuffer sb = new StringBuffer();
            if (mac != null && mac.length>0){
                for(int i=0; i<mac.length; i++) {
                    if(i!=0) {
                        sb.append("-");
                    }
                    //字节转换为整数
                    String str = Integer.toHexString(mac[i] & 0xff);
                    if(str.length()==1) {
                        sb.append("0").append(str);
                    }else {
                        sb.append(str);
                    }
                }
            }
            return sb.toString().toUpperCase();
        } catch (SocketException e) {
            e.printStackTrace();
            return null;
        }
    }
}
